package cn.yunyichina.provider.bone.service.impl;

import cn.yunyichina.provider.bone.dao.LableDao;
import cn.yunyichina.provider.bone.dao.LableDoctorDao;
import cn.yunyichina.provider.bone.entity.Lable;
import cn.yunyichina.provider.bone.entity.LableDoctor;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.commons.StringUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhuxp on 2018/2/5.
 *
 * @param
 * @Descrition
 */
@Service
@Log4j
public class DoctorLableServiceImpl {

    @Autowired
    private LableDoctorDao lableDoctorDao;
    @Autowired
    private LableDao lableDao;

    /**
     * yyt.bone.doctor.lable.list
     * @param data
     * @return
     */
    public Response listDoctorLable(String data){
        log.info("查询医生标签列表接口入参："+ data);
        Response response = new Response();
        try {
            LableDoctor lableDoctor = JsonUtils.parseObject(data,LableDoctor.class);
            if(StringUtils.isBlank(lableDoctor.getDoctorId())){
                response.setResultCodeAndMessage(Response.FAILURE_CODE,"医生id不能为空");
            }
            List<LableDoctor> lists = lableDoctorDao.select(lableDoctor);
            String resultStr ="";
            if(!lists.isEmpty()){
                StringBuffer sb = new StringBuffer();
                for(LableDoctor lableDoctor1:lists){
                    Lable lable = lableDao.selectById(lableDoctor1.getLableId());
                    sb.append(lable.getAttributeName()).append(",");
                }
                resultStr = sb.substring(0,sb.length()-1);
            }else {
                response.setResultCodeAndMessage("-2","该医生不存在标签!");
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("lable",resultStr);
            log.info("查询医生标签列表接口出参："+ jsonObject);
            response.setResultCodeAndMessageAndResult(Response.SUCCESS_CODE,Response.SUCCESS_MESSAGE, JsonUtils.toJsonString(jsonObject));
        }catch (Exception e){
            log.error("查询医生标签异常"+e);
            response.setResultCodeAndMessage(Response.FAILURE_CODE,"查询医生标签异常");
        }
        return response;
    }

    /**
     * yyt.bone.doctor.lable.save
     * @param data
     * @return
     */
    public Response saveDoctorLable(String data){
        log.info("保存医生标签入参："+data);
        Response response = new Response();
        try {
            JSONObject jsonObject =JsonUtils.parseObject(data,JSONObject.class);
            String doctorId= jsonObject.getString("doctor_id");
            String lable = jsonObject.getString("lable_id");
            String[] args =lable.split(",");
            LableDoctor lableDoctorFirst =new LableDoctor();
            lableDoctorFirst.setDoctorId(doctorId);
            List<LableDoctor> firstLables = lableDoctorDao.select(lableDoctorFirst);
            if(!firstLables.isEmpty()){
                for(LableDoctor lad:firstLables){
                    lableDoctorDao.delete(lad);
                }
            }
            for(String arg:args){
                LableDoctor lableDoctor = new LableDoctor();
                lableDoctor.setLableId(arg);
                lableDoctor.setId(PKGenerator.generateId());
                lableDoctor.setDoctorId(doctorId);
                lableDoctor.setCreateTime(DateUtils.getDateTimeStr());
                lableDoctor.setUpdateTime(DateUtils.getDateTimeStr());
                lableDoctorDao.insert(lableDoctor);
            }
            response.setResultCodeAndMessage(Response.SUCCESS_CODE,Response.SUCCESS_MESSAGE);
        }catch (Exception e){
            log.error("保存医生标签出参"+e);
            response.setResultCodeAndMessage(Response.FAILURE_CODE,"保存医生标签异常");
        }
        return response;
    }
}
